package com.wuseguang.springcloud.service.impl;

import com.wuseguang.springcloud.dao.jdbc.EvaluationModelJdbc;
import com.wuseguang.springcloud.dao.repository.EvaluationModelItemRepository;
import com.wuseguang.springcloud.dao.repository.EvaluationModelRepository;
import com.wuseguang.springcloud.entity.EvaluationModel;
import com.wuseguang.springcloud.entity.EvaluationModelItem;
import com.wuseguang.springcloud.service.EvaluationModelService;
import com.wuseguang.springcloud.util.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 评价模型表 服务实现类
 * </p>
 *
 * @author
 * @since 2020-02-12
 */
@Service
public class EvaluationModelServiceImpl implements EvaluationModelService {

    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    private EvaluationModelRepository evaluationModelRepository;
    @Autowired
    private EvaluationModelItemRepository evaluationModelItemRepository;
    @Autowired
    private EvaluationModelJdbc evaluationModelJdbc;

    @Override
    public Result addEvaluationModel(EvaluationModel evaluationModel) {
        logger.info("添加评价模型");
        //添加并获取评价模型id
        Integer id = evaluationModelRepository.save(evaluationModel).getId();

        Integer[] evaluationItemIds = evaluationModel.getEvaluationItemId();
        //循环添加
        for (int i = 0; i < evaluationItemIds.length; i++) {
            //创建对象
            EvaluationModelItem emi = new EvaluationModelItem();
            //评价项id
            emi.setEvaluationItemId(evaluationItemIds[i]);
            //评价模型id
            emi.setEvaluationModelId(id);
            evaluationModelItemRepository.save(emi);
        }
        return new Result();
    }

    @Override
    public Result findEvaluationModel(String teacherId) {
        logger.info("查询教师自己创建的评价模型");

        String sql = "select id,praise,stay_praise,name from evaluation_model where teacher_id= ?";

        List<Map<String, Object>> list = evaluationModelJdbc.queryListMap(sql, teacherId);
        //询评价模型里的评价项sql
        sql = "select id,name,fraction,icon,type from score_item si where exists(SELECT * from evaluation_model_item emi where si.id=emi.evaluation_item_id and emi.evaluation_model_id =? )";
        //循环教师的评价模型
        for (Map<String, Object> map : list) {
            //查询评价模型里的评价项
            List<Map<String, Object>> scoreItemList = evaluationModelJdbc.queryListMap(sql, map.get("id"));
            map.put("list", scoreItemList);
        }
        return new Result(list);
    }

    @Override
    public Result deleteEvaluationModel(Integer id) {
        logger.info("删除评价模型");
        try {
            evaluationModelRepository.deleteById(id);
            evaluationModelItemRepository.deleteByEvaluationModelId(id);
        } catch (Exception e) {

        }
        return new Result();
    }

    @Override
    public Result updateEvaluationModel(EvaluationModel evaluationModel) {
        logger.info("修改评价模型");
        //添加并获取评价模型id
        evaluationModelRepository.save(evaluationModel);

        evaluationModelItemRepository.deleteByEvaluationModelId(evaluationModel.getId());

        Integer[] evaluationItemIds = evaluationModel.getEvaluationItemId();
        //循环添加
        for (int i = 0; i < evaluationItemIds.length; i++) {
            //创建对象
            EvaluationModelItem emi = new EvaluationModelItem();
            //评价项id
            emi.setEvaluationItemId(evaluationItemIds[i]);
            //评价模型id
            emi.setEvaluationModelId(evaluationModel.getId());
            evaluationModelItemRepository.save(emi);
        }
        return new Result();
    }
}
